﻿using PlayerRoles;
using PluginAPI.Core;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Utils.ModuleKiller
{
    /// <summary>
    /// 
    /// </summary>
    public class KillLeaderboard
    {
        /// <summary>
        /// 
        /// </summary>
        public static Dictionary<string, KillerInfo> KillBorad { get; private set; } = new Dictionary<string, KillerInfo>();


        public static uint maxShow = 4;

        /// <summary>
        /// 获取
        /// </summary>
        public static string GetKillBoardSCP()
        {
            var infoSCPs = KillBorad
                .ToList()
                .FindAll(e => e.Value.Player.Team == Team.SCPs);

            StringBuilder build = new StringBuilder();
            {
                build.AppendLine($"<color=red>💀</color> 击杀排行榜");
                build.AppendLine("--------");
            }

            if (infoSCPs.Count == 0)
            {
                for (int i = 0; i < maxShow; i++)
                {
                    build.AppendLine("");
                }

                return build.ToString();
            }



            infoSCPs.Sort((a, b) => b.Value.KillHuman.CompareTo(a.Value.KillHuman));

            bool isFull = infoSCPs.Count >= maxShow;
            if (isFull)
            {
                for (int i = 0; i < maxShow; i++)
                {
                    build.AppendLine($"{infoSCPs[i].Value.KillHuman} [<color=red>{infoSCPs[i].Value.Player.Role.ToString().ToUpper().PadRight(7)}</color>]");
                }

                return build.ToString();
            }

            for (int i = 0; i < infoSCPs.Count; i++)
            {
                build.AppendLine($"{infoSCPs[i].Value.KillHuman} [<color=red>{infoSCPs[i].Value.Player.Role.ToString().ToUpper().PadRight(7)}</color>]");
            }

            for (int i = infoSCPs.Count; i < maxShow; i++)
            {
                build.AppendLine("");
            }

            return build.ToString();
        }

        /// <summary>
        /// 获取|角色预设值
        /// </summary>
        /// <param name="player"></param>
        /// <returns></returns>
        public static KillerInfo Get(Player player)
        {
            bool isGet = KillBorad.TryGetValue(player.UserId, out KillerInfo info);
            if (!isGet)
            {
                info = new KillerInfo(player);
                KillBorad.Add(player.UserId, info);
            }

            return info;
        }


        /// <summary>
        /// 
        /// </summary>
        public static void Clear()
        {
            KillBorad.Clear();
        }
    }
}
